package com.lk.springboot.component;

import com.lk.springboot.service.TempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lk
 * @date 2020/1/20 0020 17:31
 */

@Component
public class ClearDataJob {
    private static final Logger logger = LoggerFactory.getLogger(ClearDataJob.class);
    //注入service对象 方便调用
    @Resource
    private TempService tempService;

    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void clearDataJob() {
        logger.info("---------定时任务开始执行---------" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
        clearData(tempService);
        logger.info("---------定时任务执行成功---------" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
    }

    /**
     * 清理数据
     *
     * @param tempService
     */
    private static void clearData(TempService tempService) {
        try {
            tempService.clearTable();//调用service层的方法 删除数据库数据
        } catch (Exception e) {
            logger.error("清理数据失败，失败原因：" + e.getMessage());
        }
    }
}